package com.sakiko.audiobook.user.strategy.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sakiko.audiobook.model.user.UserPaidAlbum;
import com.sakiko.audiobook.user.mapper.UserPaidAlbumMapper;
import com.sakiko.audiobook.user.strategy.ItemTypeStrategy;
import com.sakiko.audiobook.vo.user.UserPaidRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: sakiko
 * time: 2025/11/9 0:23
 * description: 针对 专辑订单的处理
 * <bean id="1001" class="com.sakiko.audiobook.user.strategy.impl.AlbumStrategy"></bean>
 */
@Service("1001") // 重命名 bean.id 对应好 userPaidRecordVo.getItemType()
public class AlbumStrategy implements ItemTypeStrategy {

    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;

    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        // 购买专辑 (判断专辑对象是否存在)
        UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getUserId, userPaidRecordVo.getUserId())
                .eq(UserPaidAlbum::getAlbumId, userPaidRecordVo.getItemIdList().get(0)));
        if (null != userPaidAlbum) return; // 购买过
        // 购买专辑
        userPaidAlbum = new UserPaidAlbum();
        userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
        userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));
        userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
        // 保存记录
        userPaidAlbumMapper.insert(userPaidAlbum);
    }
}
